package com.invengo.module_check.ui.fragment.bookdown.fragment;

import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentBookDownByMultipleLayerBinding;
import com.invengo.module_check.ui.fragment.widget.SelectShelfDialog;

import java.io.Serializable;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus._CacheTag;
import me.goldze.mvvmhabit.utils.KLog;

public class BookDownByMultipleShelfFragment extends BaseFragment<FragmentBookDownByMultipleLayerBinding, BookDownViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_book_down_by_multiple_layer;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void changeModel() {
        RxBus.getDefault().postSticky(AnalyzeImpl.TO_SHELF);
    }
    private void changeForce(boolean isFirst){
        new Handler().postDelayed(() -> {
            KLog.e("2秒后执行切换操作"+isFirst);
            try {
                if (isFirst)
                    binding.first.requestFocus();
                else
                    binding.second.requestFocus();
            } catch (Exception e) {
               e.printStackTrace();
            }
        },400);
    }
    MaterialDialog downloadDiaog;
    @Override
    public void initViewObservable() {
        viewModel.uiChage.multiChangeEvent.observe(this, b -> changeForce(b));
        viewModel.uiChage.nextEvent.observe(this, bookBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS,(Serializable) (viewModel.getFialBooks().isEmpty()? bookBeans:viewModel.getFialBooks()));
            bundle.putSerializable(BundleKey.KEY_FAIL_BOOKS, (Serializable) viewModel.getFialBooks());
            bundle.putSerializable(BundleKey.KEY_ACTION, ActionArgs.ACTION_DOWN);
            bundle.putBoolean(KEY_OP_STATE, bookBeans != null&&viewModel.getFialBooks().isEmpty());
            bundle.putBoolean(BundleKey.KEY_IS_SHELF_RESULT, true);
            bundle.putString(BundleKey.KEY_TITLE, "下架");
            NavHostFragment.findNavController(this).navigate(ActionArgs.ACTION_DOWN.getResultAction(), bundle);
        });
        viewModel.uiChage.selectShelfEvent.observe(this, bean -> SelectShelfDialog.getInstance().showDialog(getActivity(), bean, bean1 -> {
            viewModel.handlerShelf(bean1);
        }));

        viewModel.uiChage.downloadDialogEvent.observe(this, aBoolean -> {
            if (downloadDiaog == null) initDownloadDialog();
            if (aBoolean) {
                downloadDiaog.setProgress(0);
                downloadDiaog.show();
            } else
                downloadDiaog.dismiss();
        });

        viewModel.uiChage.downloadProgressEvent.observe(this, integer -> {
            if (downloadDiaog != null && downloadDiaog.isShowing())
                if (integer == -1) {
                    downloadDiaog.setProgress(0);
                } else {
                    downloadDiaog.incrementProgress(integer);
                }
        });
        viewModel.uiChage.downloadContentEvent.observe(this, str -> {
            if (downloadDiaog != null && downloadDiaog.isShowing())
                downloadDiaog.setContent(str);
        });
    }
    private void initDownloadDialog() {
        downloadDiaog = new MaterialDialog.Builder(getActivity())
                .title("图书下架中")
                .cancelable(false)
                .content("正在下架图书数据...")
                .contentGravity(GravityEnum.CENTER)
                .progress(false, 100, true)
                .build();
    }

}
